//
//  CNM_ExNavigationController.swift
//  CNMTools
//
//  Created by MrHong on 2023/11/10.
//

import UIKit

public extension UINavigationController {
    // 自定义左侧返回按钮
    func createLeftBackItem(_ viewController: UIViewController, backIcon: String, backLeftMargin: CGFloat = -11.0, target: Any?, action: Selector?) {
        let leftBarButtonItem = UIBarButtonItem(image: UIImage(named: backIcon), style: .plain, target: target, action: action)
        
        if #available(iOS 11.0, *) { // ios11 以上偏移
            leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: backLeftMargin, bottom: 0, right: 0) // 调节边距
            viewController.navigationItem.leftBarButtonItem = leftBarButtonItem
        } else {
            let nagetiveSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            nagetiveSpacer.width = backLeftMargin // 这个值可以根据自己需要自己调整
            viewController.navigationItem.leftBarButtonItems = [nagetiveSpacer, leftBarButtonItem]
        }
    }
}
